import config from './config.js';

function schedule(name,nodeWidth,nodeHeight,rowSpace,colSpace){
	const {numberLeft,numberRight,numberCenter,numberOther} = config[name];
	const edges = [];
	let nodes = [];
	
	const getNodesCenter = () =>{
		let arr = [];
		let x = 0;
		let y = 0;
		numberCenter.forEach((v,i)=>{
			let item = {
				id:v+'',
				label:`场次号：${v+''}`,
				x:x
			};
			y += (nodeHeight+colSpace);
			item.y = y;
			arr.push(item)
		})
		return arr;
	}
	const nodesCenter = getNodesCenter();
	
	const getNodesRightOrLeft = (isLeft) => {
		let numbers = isLeft?JSON.parse(JSON.stringify(numberLeft)):JSON.parse(JSON.stringify(numberRight));
		let arr = [];
		let arr_col = [];
		let centerX = nodesCenter[0].x;
		let centerY = nodesCenter[0].y;
		let sourceAnchor = 0;
		let targetAnchor = 1
		numbers.forEach((d,k)=>{
			let x = 0;
			if(isLeft){
				x = centerX - ((nodeWidth+rowSpace)*(k+1));
			}else{
				x = centerX + ((nodeWidth+rowSpace)*(k+1));
				sourceAnchor = 1;
				targetAnchor = 0
			}
			let y = centerY;
			arr_col.push([])
			d.forEach((v,i)=>{
				let item = {
					id:v+'',
					label:`场次号：${v+''}`,
					x:x
				};
				if(k == 0){
					if(d.length == nodesCenter.length){
						y = nodesCenter[i].y;
						edges.push({ 
							source:nodesCenter[i].id, 
							target: item.id,
							sourceAnchor,targetAnchor
						})
					}else if (d.length*2 == nodesCenter.length){
						y = (nodesCenter[i*2].y + nodesCenter[i*2+1].y)/2;
						edges.push({
							source:nodesCenter[i*2].id, 
							target: item.id,
							sourceAnchor,targetAnchor
						})
						edges.push({
							source:nodesCenter[i*2+1].id, 
							target: item.id,
							sourceAnchor,targetAnchor
						})
					}else{
						
					}
				}else{
					if(d.length == arr_col[k-1].length){
						y = arr_col[k-1][i].y
						edges.push({
							source:arr_col[k-1][i].id, 
							target: item.id,
							sourceAnchor,targetAnchor
						})
					}else if (d.length*2 == arr_col[k-1].length){
						y = (arr_col[k-1][i*2].y + arr_col[k-1][i*2+1].y)/2	
						edges.push({
							source:arr_col[k-1][i*2].id, 
							target: item.id,
							sourceAnchor,targetAnchor
						})
						edges.push({
							source:arr_col[k-1][i*2+1].id, 
							target: item.id,
							sourceAnchor,targetAnchor
						})
					}else{
						
					}	
				}
				item.y = y;
				arr.push(item);
				arr_col[k].push(item)
			})
		})
		return arr;
	}
	const getNodesTop = () =>{
		const colsCenter = '正赛';
		const colsLeft = [];
		const colsRight = [];
		numberLeft.forEach((v,i)=>{
			colsLeft.push(`败部第${(i+1)+''}轮`)
		})
		numberRight.forEach((v,i)=>{
			colsRight.push(`胜部第${(i+1)+''}轮`)
		})
		
		let y = 0;
		let colsLeftX = 0;
		let colsRightX = 0;
		let nodeConfig = {
			y:y,
			type:'rect',
			style:{
				stroke:'#B37D02',
			},
			labelCfg:{
				style:{
					fill:'#B37D02'
				}
			},
			stateStyles: {
				hover: {},
				selected: {
					stroke:'#B37D02',
					shadowColor:'#ffffff',
					lineWidth:'1'
				},
			},
		}
		let arr = [
			{
				id:'topCenter',
				label:colsCenter,
				x:0,
				...nodeConfig
			}
		];
		colsLeft.forEach((v,i)=>{
			colsLeftX -= (nodeWidth+rowSpace)
			arr.push({
				id:'topLeft'+(i+1),
				label:v,
				x:colsLeftX,
				...nodeConfig
			})
		})
		colsRight.forEach((v,i)=>{
			colsRightX += (nodeWidth+rowSpace)
			arr.push({
				id:'topRight'+(i+1),
				label:v,
				x:colsRightX,
				...nodeConfig
			})
		})
		
		return arr;
	}
	
	const nodesRight = getNodesRightOrLeft(0);
	const nodesLeft = getNodesRightOrLeft(1);
	const nodesTop = getNodesTop();
	nodes = [].concat(nodesCenter).concat(nodesRight).concat(nodesLeft).concat(nodesTop);
	
	const getNodeOther = () => {
		if(!numberOther || numberOther.length == 0){
			return []
		};
		let arr = [];
		numberOther.forEach(v=>{
			let { position,positionSourceId,id,edgesSourceId } = v;
			positionSourceId += '';
			id += '';
			let item = {
				id:id,
				label:`场次号：${id}`,
			}
			let nodeIndex = nodes.findIndex(v=>v.id == positionSourceId);
			let { x,y }= nodes[nodeIndex];
			switch(position) {
			  case 'left':
			    item.y = y;
			    item.x = x - (nodeWidth+rowSpace);
			    break;
			  case 'bottom':
			      item.y = y + (nodeHeight + colSpace);
			      item.x = x;
			      break;
			  default:
			      
			}
			if(name == '4single'){
				item.x += ((nodeWidth+rowSpace)/2)
			}
			if(edgesSourceId){
				edgesSourceId.forEach(sourceId=>{
					sourceId += '';
					let _sourceNode = nodes.find(v=>v.id == sourceId);
					let sourceAnchor = 0;
					let targetAnchor = 0;
					if(_sourceNode.x > item.x){
						if(_sourceNode.y < item.y){
							sourceAnchor = 3;
							targetAnchor = 1;
						}
					}
					if(_sourceNode.x < item.x){
						if(_sourceNode.y < item.y){
							sourceAnchor = 3;
							targetAnchor = 0;
						}
					}
					edges.push({
						target:id,
						source:sourceId,
						sourceAnchor,
						targetAnchor,
						style: {
						        endArrow: true,
						      }
					})
				})
			}else{
				edges.push({
					target:id,
					source:positionSourceId,
					sourceAnchor:0,
					targetAnchor:1,
					style: {
					        endArrow: true,
					      }
				})
			}
			arr.push(item)
		})
		return arr
	}
	const nodesOther = getNodeOther();
	nodes = nodes.concat(nodesOther)

	return {
		nodes:nodes,
		edges: edges
	}
}

export default schedule;